package user.models;

import helpers.GeneralHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import com.google.appengine.api.NamespaceManager;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

import entities.User;

public class UserModel {
	private Objectify ofy;
	
	private Cache cache = null;
	
	private String prefix = "user_user_";
	
	private static UserModel instance = null;
	
	public static UserModel getInstance() {
		if (instance == null) {
			instance = new UserModel();
		}
		
		return instance;
	}
	
	private UserModel() {
		try {
			ObjectifyService.register(User.class);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		cache = GeneralHelper.getCache();
		ofy = GeneralHelper.getObjectify();
	}
	
	public List<User> list(String ns, int page, int limit) {
		String currentNs = NamespaceManager.get();
		if (!currentNs.equals(ns)) {
			NamespaceManager.set(ns);
		}
		
		List<User> users = new ArrayList<User>();
		
		String cacheName = prefix + ns + "_page_" + page + "_limit_" + limit;
		boolean isCache = false;
		
		if (cache != null) {
			try {
				users = (List<User>) cache.get(cacheName);
				if (users != null) isCache = true;
			} catch (Exception e) {
			}
		}
		
		if (!isCache) {
			int start = (page - 1) * limit;
			users = ofy.query(User.class).limit(limit).offset(start).list();
			
			if (cache != null) {
				cache.put(cacheName, users);
			}
		}
		
		NamespaceManager.set(currentNs);
		
		return users;
	}
	
	public void insert(String ns, User user) {
		String currentNs = NamespaceManager.get();
		if (!currentNs.equals(ns)) {
			NamespaceManager.set(ns);
		}
		
		try {
			String cacheName = prefix + ns + "_" + user.getUsername();
			cache.put(cacheName, user);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		ofy.put(user);
		
		NamespaceManager.set(currentNs);
	}
	
	public void delete(String ns, User user) {
		String currentNs = NamespaceManager.get();
		if (!currentNs.equals(ns)) {
			NamespaceManager.set(ns);
		}
		
		ofy.delete(user);
		
		NamespaceManager.set(currentNs);
	}
	
	public User getUser(String ns, String id) {
		String currentNs = NamespaceManager.get();
		if (!currentNs.equals(ns)) {
			NamespaceManager.set(ns);
		}
		
		User user = new User();
		try {
			user = ofy.get(new Key<User>(User.class, id));
		} catch (Exception e) {
			user = null;
		}
		
		NamespaceManager.set(currentNs);
		
		return user;
	}
	
	public Map<String, User> getByListId(String ns, List<String> ids) {
		String currentNs = NamespaceManager.get();
		if (!currentNs.equals(ns)) {
			NamespaceManager.set(ns);
		}
		
		Map<String, User> users = ofy.get(User.class, ids);
		
		NamespaceManager.set(currentNs);
		
		return users;
	}
	
	public User getByUsername(String ns, String username) {
		String currentNs = NamespaceManager.get();
		if (!currentNs.equals(ns)) {
			NamespaceManager.set(ns);
		}
		
		User user = new User();
		
		String cacheName = prefix + username;
		boolean isCached = false;
		
		if (cache != null) {
			try {
				user = (User) cache.get(cacheName);
				if (user != null) cache.get(cacheName);
			} catch (Exception e) {
				user = null;
				// TODO: handle exception
			}
		}
		
		if (!isCached) {
			try {
				user = ofy.query(User.class).filter("username", username).get();
			} catch (Exception e) {
				user = null;
			}
			
			if (cache != null && user != null) {
				cache.put(cacheName, user);
			}
		}
		
		NamespaceManager.set(currentNs);
		
		return user;
	}
	
	public boolean checkUserExists(String ns, String username) {
		boolean exists = false;
		User user = getByUsername(ns, username);
		if (user != null) {
			exists = true;
		}
		return exists;
	}
}
